package com.eclipse.e0113_网络编程.socket_文件下载;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class FileClient {

	static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) throws Exception {
		
		Socket socket = new Socket("172.20.10.2", 9001);

		InputStream inputStream = socket.getInputStream();
		OutputStream outputStream = socket.getOutputStream();
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

		
		
		// 读取文件名
		String fileName = "";
		do {
			fileName = bufferedReader.readLine();
			System.out.println(fileName);
		} while (!"".equals(fileName));

		// 选择要下载的文件
		System.out.println("请输入你要下载的文件名：");
		String downFilename = scanner.nextLine();
		outputStream.write((downFilename + "\r\n").getBytes());

		//10字节的byte数组
		byte[] fileContent = new byte[10];
		//读到的字节长度
		int byteCount = 0;

		//保存到哪个目录
		FileOutputStream fileOut = new FileOutputStream("/Users/liuzhuangzhuang/iotest/q/" + downFilename);

		do {
			//读到要下载文件的字节长度
			byteCount = inputStream.read(fileContent);
			if (byteCount != -1) {
				//写出
				fileOut.write(fileContent, 0, byteCount);
			}
		} while (byteCount != -1);

		fileOut.close();

		inputStream.close();

		socket.close();

	}

}
